$().ready(function() {
	loadType();
	loadTypeBySource()
	loadcompany();
	loadcustname();
	validateRule();
});

$.validator.setDefaults({
	submitHandler : function() {
		console.log("111")
		save();
	}
});
function save() {
	$.ajax({
		cache : true,
		type : "POST",
		url : "/itm/project/save",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);
			} else {
				parent.layer.alert(data.msg)
			}
		}
	});
}

function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			name : {
				required : true
			},
			itmProTypeid : {
				required : true
			},
			itmManagerId : {
				required : true
			},
			deptId : {
				required : true
			},
			custName : {
				required : true
			},
			userNames:{
				required : true
			}
		},
		messages : {
			name : {
				required : icon + "请输入项目名称"
			},
			itmProTypeid : {
				required : icon + "请选择项目类型"
			},
			itmManagerId : {
				required : icon + "请选择项目负责人"
			},
			deptId : {
				required : icon + "请选择所属业务部门"
			},
			deptId : {
				required : icon + "请输入客户名称"
			},
			userNames:{
				required : icon + "请选择项目组成员"
			}
		}
	})
}
function loadType(){
	var html = "";
	$.ajax({
		url : '/common/dict/list/itm_project',
		success : function(data) {
			//加载数据
			for (var i = 0; i < data.length; i++) {
				html += '<option value="' + data[i].value + '">' + data[i].name + '</option>'
			}
			$(".chosen-select").append(html);
			$(".chosen-select").chosen({
				maxHeight : 200
			});
			//点击事件
			$('.chosen-select').on('change', function(e, params) {
				console.log(params.selected);
				var opt = {
					query : {
						type : params.selected,
					}
				}
				$('#exampleTable').bootstrapTable('refresh', opt);
			});
		}
	});
}
function loadTypeBySource(){
	var html = "";
	$.ajax({
		url : '/common/dict/list/itm_project_source',
		success : function(data) {
			//加载数据
			for (var i = 0; i < data.length; i++) {
				html += '<option value="' + data[i].value + '">' + data[i].name + '</option>'
			}
			$(".chosen-select-source").append(html);
			$(".chosen-select-source").chosen({
				maxHeight : 200
			});
			//点击事件
			$('.chosen-select').on('change', function(e, params) {
				console.log(params.selected);
				var opt = {
					query : {
						type : params.selected,
					}
				}
				$('#exampleTable').bootstrapTable('refresh', opt);
			});
		}
	});
}

function loadcompany(){
	var html = "";
	$.ajax({
		url : '/system/company/getCompanyList',
		success : function(data) {
			//加载数据
			for (var i = 0; i < data.length; i++) {
				html += '<option value="' + data[i].id + '">' + data[i].name + '</option>'
			}
			$(".chosen-select-complanyId").append(html);
			$(".chosen-select-complanyId").chosen({
				maxHeight : 200,
				search_contains: true
			});

			//点击事件
			$('.chosen-select-complanyId').on('change', function(e, params) {
				console.log(params.selected);
				var opt = {
					query : {
						type : params.selected,
					}
				}
				$('#exampleTable').bootstrapTable('refresh', opt);
				$.ajax({
					url :"/system/company/get/"+params.selected,
					data:null,
					type:"get",
					success : function(data) {
						console.log(getdate());
						var companySort = data.companySort+"-"+getdate();
						$("input[name='itmNumber']").val(companySort);
					}
				});
			});
		}
	});
}

function loadcustname(){
	var html = "";
	$.ajax({
		url : '/itm/projectCustname/customlist',
		success : function(data) {
			//加载数据
			for (var i = 0; i < data.length; i++) {
				html += '<option value="' + data[i].name + '">' + data[i].name + '</option>'
			}
			$(".chosen-select-custName").append(html);
			$(".chosen-select-custName").chosen({
				maxHeight : 200,
				search_contains: true
			});

			//点击事件
			$('.chosen-select-custName').on('change', function(e, params) {
				console.log(params.selected);
				var opt = {
					query : {
						type : params.selected,
					}
				}
				$('#exampleTable').bootstrapTable('refresh', opt);
			});
		}
	});
}


function getdate() {
	var date=new Date();

	var year=date.getFullYear();
	var month=date.getMonth()+1;
	var day=date.getDate();

	var hour=date.getHours();
	var minute=date.getMinutes();
	var second=date.getSeconds();
	var millis =  randomNum(1,1000);
	//这样写显示时间在1~9会挤占空间；所以要在1~9的数字前补零;
	return year+""+isdate(month)+isdate(day)+isdate(hour)+isdate(minute)+isdate(second)+millis;
}

function isdate(values) {
	if(values <10){
		values = "0"+values;
	}
	return values;
}

//生成从minNum到maxNum的随机数
function randomNum(minNum,maxNum){
	switch(arguments.length){
		case 1:
			return parseInt(Math.random()*minNum+1,10);
			break;
		case 2:
			return parseInt(Math.random()*(maxNum-minNum+1)+minNum,10);
			break;
		default:
			return 0;
			break;
	}
}


var openUser = function(){
	layer.open({
		type:2,
		title:"选择人员",
		area : [ '300px', '450px' ],
		content:"/sys/user/treeView"
	})
}

function loadUser(userIds,userNames){
	$("#userIds").val(userIds);
	$("#userNames").val(userNames);
}